package com.fubowen.reader.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.fragment.app.activityViewModels
import com.fubowen.reader.databinding.ReaderSettingBinding
import com.fubowen.reader.ui.fragment.ThemeFragment
import com.fubowen.reader.vm.ReaderViewModel

class ReaderSettingFragment : ThemeFragment() {

    private lateinit var binding: ReaderSettingBinding
    private val readerViewModel by activityViewModels<ReaderViewModel>()

    override var isDialog = true
    override var height = -1f

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = ReaderSettingBinding.inflate(inflater)
        binding.lmValue.setOnSeekBarChangeListener(onSeekBarChanged())
        binding.lmEye.setOnClickListener { themeViewModel.settProtectEyeMode() }
        binding.fsAdd.setOnClickListener { readerViewModel.addTextSize(1f) }
        binding.fsSub.setOnClickListener { readerViewModel.subTextSize(1f) }
        binding.lhXs.setOnClickListener { readerViewModel.setLineSpacingMultiplier(1.0F) }
        binding.lhSm.setOnClickListener { readerViewModel.setLineSpacingMultiplier(1.5F) }
        binding.lhMd.setOnClickListener { readerViewModel.setLineSpacingMultiplier(2.0F) }
        binding.lhLg.setOnClickListener { readerViewModel.setLineSpacingMultiplier(2.5F) }
        binding.readerBg0.setOnClickListener { themeViewModel.setTheme("白色主题") }
        binding.readerBg1.setOnClickListener { themeViewModel.setTheme("淡黄主题") }
        binding.readerBg2.setOnClickListener { themeViewModel.setTheme("淡绿主题") }
        binding.readerBg3.setOnClickListener { themeViewModel.setTheme("淡蓝主题") }
        binding.readerFySx.setOnClickListener { themeViewModel.setTheme("白色主题")}
        binding.readerFyZy.setOnClickListener { themeViewModel.setTheme("白色主题")}
        return binding.root
    }

    private fun onSeekBarChanged() = object : SeekBar.OnSeekBarChangeListener {

        override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
            readerViewModel.setScreenBrightness(progress)
        }

        override fun onStartTrackingTouch(seekBar: SeekBar?) {}

        override fun onStopTrackingTouch(seekBar: SeekBar?) {}
    }

}